using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.GridEX;

namespace Janus.NorthwindDemo2007
{
	public partial class OrdersGridEXView
	{
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

        }
		protected override void OnFolderViewChanged()
		{
            base.OnFolderViewChanged();
			GridEXLayout layout = this.GridEX.FindLayout(this.FolderView.LayoutName);
			layout.SetDataBinding(this.DataSource, "Orders");
			this.GridEX.CurrentLayout = layout;
			if (this.GridEX.Hierarchical & DataProvider != null)
			{
				((OrdersDataProvider)DataProvider).IncludeDetails = true;
                GridEXColumn colCategories = this.GridEX.RootTable.ChildTables[0].Columns["OrderDetails_ProductCategories"];
                colCategories.HasValueList = true;
                colCategories.ValueList.PopulateValueList(NorthwindApp.NorthwindApplication.GetProductCategoryList().DefaultView, "CategoryID", "CategoryName", "SmallIcon", Color.Magenta, new Size(16, 16));
			}
			this.GridEX.DropDowns["Customers"].SetDataBinding(NorthwindApp.NorthwindApplication.GetCustomersList(), "");
			this.GridEX.DropDowns["Employees"].SetDataBinding(NorthwindApp.NorthwindApplication.GetEmployeeList(), "");
			if (this.GridEX.Hierarchical)
			{
				this.GridEX.DropDowns["Products"].SetDataBinding(NorthwindApp.NorthwindApplication.GetProductList(), "");
			}
			this.LoadShipViaValueList();


			//We need to call Refetch method if valuelist or dropdowns are populated
			//after the datasource in the grid is populated
			this.GridEX.Refetch();
			this.EnableCommands();


		}
		private void LoadShipViaValueList()
		{
			DataView shippers = new DataView(NorthwindApp.NorthwindApplication.GetShippersList());
			this.GridEX.RootTable.Columns["ShipVia"].ValueList.PopulateValueList(shippers, "ShipperID", "CompanyName", "SmallIcon", Color.Magenta, new Size(16, 16));
		}

		private object CalculateDetailValue(GridEXRow row)
		{
			int quantity = 0;
			float discount = 0F;
			decimal unitPrice = 0M;
			try
			{
				quantity = System.Convert.ToInt32(row.Cells["Quantity"].Value);
			}
			catch
			{
			}
			try
			{
				discount = System.Convert.ToSingle(row.Cells["Discount"].Value);
			}
			catch
			{
			}
			try
			{
				unitPrice = System.Convert.ToDecimal(row.Cells["UnitPrice"].Value);
			}
			catch
			{
			}
			return quantity * (float)unitPrice * (1 - discount);

		}
		private void GridEX_LoadingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
		{
			if (e.Row.RowType == RowType.Record && e.Row.Table.Key == "OrdersOrderDetails")
			{
				e.Row.Cells["DetailPrice"].Value = CalculateDetailValue(e.Row);
			}
		}
		protected override void OnDataProviderChanged()
		{
			base.OnDataProviderChanged();
			if (this.GridEX.Hierarchical & DataProvider != null)
			{
				((OrdersDataProvider)DataProvider).IncludeDetails = true;
			}
		}


		public override void Edit()
		{
			if (this.DataProvider.Update())
			{
				foreach (GridEXSelectedItem item in this.GridEX.SelectedItems)
				{
					if (item.RowType == RowType.Record && (item.Table == GridEX.RootTable))
					{
						OrderDataForm orderDialog = new OrderDataForm();
						orderDialog.Edit(((DataRowView)(item.GetRow().DataRow)).Row);
					}
				}
			}
		}

		private void GridEX_SelectionChanged(object sender, EventArgs e)
		{
			GridEXRow currentRow = null;
			currentRow = GridEX.GetRow();

		}

	}

} //end of root namespace